define(function(require, exports, module){
	require("resource/js/easyui/themes/icon.css");
	require("resource/js/easyui/easyloader.js");
	require("resource/js/component/rules.js");
	var backDlg = require("resource/js/component/backDialog");
	var easyutil = require("resource/js/component/easyutil.js");
	var ajax = require("resource/js/component/ajax.js");
	var combobox = require("resource/js/component/easycombobox.js");
	
	var config = {
		type:"tablesearch",
		name:"流程表",
		column:[[
	         {field:'tableKey',title:'表编码',width:100,align:'center',height:50},
	         {field:'shortNameCh',title:'说明信息',width:100,align:'center',height:50},
	         {field:'businessId',title:'业务ID',width:100,align:'center',height:50,hidden:true},
	         {field:'businessName',title:'业务名称',width:100,align:'center',height:50},
	         {field:'parentTableKey',title:'主表名',width:100,align:'center',height:50},
	         {field:'ordernum',title:'排序',width:100,align:'center',height:50},
	         {field:'encrypt',title:'是否加密',width:100,align:'center',height:50},
	         {field:'isCreated',title:'创建情况',width:100,align:'center',height:50,hidden:true},
	         {field:'isCreatedText',title:'创建情况',width:100,align:'center',height:50,formatter:function(value,row,index){
	        	 if(row.isCreated==0){
	        		 return "<span style='color:#ccc'>已创建</span>";
	        	 }else{
	        		 return "<span style='color:red'>未创建</span>";
	        	 }
	         }}
		]]
	};
	var dgutil = undefined;
	var datagridconfig = undefined;
	var sysDept="sys";
	function initPage(){
		initBussCombobox('search_business_id');
		//初始化主表datagrid
		dgutil = new easyutil.ajaxDataGrid(datagridconfig);
	}
	function initBussCombobox(ele){
		easyutil.ajaxHandler(BASEPATH+'/base/authoriza/conmonData/queryData.htm',{queryId:"act_buss",param:sysDept},function(code,msg,data){
			combobox.init({ ele:"#"+ele,valueField:'id',remote:false, textField:'name'}).load(data);
		});
	}
	var addDialogInit = function(baseUrl){
		$("#adddlg").dialog({
			iconCls: 'icon-save',
			title:config.name+'增加',
			href:baseUrl,
			onLoad:function(){
				initBussCombobox('add_business_id');
				easyutil.ajaxHandler(BASEPATH+'/base/authoriza/conmonData/queryData.htm',{
					queryId:"basecode",
					param:"itemiskey"
					},function(code,msg,data){
						combobox.init({ 
							ele:"#encrypt",
							valueField:'id',
							textField:'text'
						}).load(data);
					});
			},
			buttons: [{
				text:'保存',
				iconCls:'icon-ok',
				accessKey:{akey:18,mkey:83},
				handler:function(){
					using(["form"],function(){
						if($("#addform").form("validate")){
							easyutil.ajaxHandler(BASEPATH+'/oa/act/actExTable/saveActExTable.htm',$("#addform").serializeJson(),function(){
								dgutil.pager();
								$("#adddlg").dialog("close");
							});
						}
					});
				}
			},{
				text:'取消',
				addClass:'l-btn-red',iconCls:'icon-not',
				accessKey:{akey:18,mkey:67},
				handler:function(){
					$('#adddlg').dialog('close');
				}
			}]
		});
		$("#adddlg").dialog("open");
	}
	var updDialogInit = function(baseUrl,row){
		$("#adddlg").dialog({
			iconCls: 'icon-save',
			title:config.name+'修改',
			href:baseUrl,
			onLoad:function(){
				initCombobox('upd_business_id');
				setTimeout(function(){
					using(["form"],function(){
						$('#updform').form('load',row);
						$("#upd_business_id").combobox("setValue",row.businessId);
					});
				},200);
			},
			buttons: [{
				text:'保存',
				iconCls:'icon-ok',
				accessKey:{akey:18,mkey:83},
				
				handler:function(){
					using(["form"],function(){
						if($("#updform").form("validate")){
							easyutil.ajaxHandler(BASEPATH+"/oa/act/actExTable/updateActExTable.htm",$("#updform").serializeJson(),function(){
								dgutil.pager();
								$("#adddlg").dialog("close");
							});
						}
					});
				}
			},{
				text:'取消',
				addClass:'l-btn-red',iconCls:'icon-not',
				accessKey:{akey:18,mkey:67},
				handler:function(){
					$('#adddlg').dialog('close');
				}
			}]
		});
		$("#adddlg").dialog("open");
	}
	
	var delInit = function(baseUrl,rows){
		var checks = "";
		var newsContentOldPath = "";
		for(var key in rows){
			if(key==rows.length-1){
				checks += rows[key].tableKey;
			}else{
				checks += rows[key].tableKey+",";
			}
		}
		easyutil.ajaxHandler(baseUrl,{checkeds:checks},function(){
			dgutil.pager();
		});
	}
	function editTableCols(baseUrl,row){
		if(indexApp){
			var urlTool = require("resource/js/component/urlTool.js");
			var url = baseUrl+"?tableKey="+row.tableKey+"&businessId="+row.businessId+"&pid="+urlTool.getParam("pid");
			indexApp.exec("close","字段配置");
			indexApp.exec("add",{name:"字段配置",url:url});
		}
	}

	var setProcessPerson = function(baseUrl,row){ 
			 var urlTool = require("resource/js/component/urlTool.js");
			 var url = baseUrl+"?tableKey="+row.tableKey+"&businessId="+row.businessId;
			backDlg.openBackDialog({
				href:url,
				title:'字段配置'
			});
	};

	function createTable(baseUrl,row){
		if(row.isCreated == 0){
			$.messager.show({
				title:'提示信息',
				msg:'当前选择的表已经创建，不能重复创建操作！',
				timeout:3000,
				showType:'slide'
			});
		}else{
			easyutil.ajaxHandler(baseUrl,{tableKey:row.tableKey},function(code,msg,data){
				dgutil.pager();
			});
		}
	}
	module.exports = {
		trans:function(id){
			var buttons ={ 
				title:{
					_class:"",
					text:"流程配置信息列表"
				},
				btn:[
			       {
				iconCls: 'icon-search',
				url:'/oa/act/actExTable/queryActExTableByPage.htm',
				text:"查询",
				addClass:'l-btn-display',
				handler: function(){dgutil.pager(1);}
			},{
				iconCls: 'icon-add',
				addClass:'l-btn-display',
				text:"添加",
				url:'/view/oa/act/ex/table/add',
				handler: addDialogInit
			},
			 {
				iconCls: 'icon-edit',
				url:'/view/oa/act/ex/table/upd',
				text:"修改",
				addClass:'l-btn-display',
				needRow:true,
				handler: updDialogInit
			},
			 {
				iconCls: 'icon-remove',
				url:'/oa/act/actExTable/deleteActExTable.htm',
				text:"删除",
				addClass:'l-btn-display',
				needRows:true,
				isCheck:true,
				handler: delInit
			},
			 {
				iconCls: 'icon-icon1_72',
				url:'/view/oa/act/ex/cols/colssearch',
				text:"字段配置",
				addClass:'l-btn-display',
				needRow:true,
				handler: setProcessPerson
				//handler: editTableCols
			},
			 {
				iconCls: 'icon-add',
				url:'/oa/act/actExTable/addTableToDbByTableKey.htm',
				text:"生成表",
				addClass:'l-btn-display',
				needRow:true,
				handler: createTable
			}
			]};
			datagridconfig = {
					fitColumns:true,
					singleSelect:true,
					striped:true,
					autoRowHeight:false,
					fit: true,
					url:BASEPATH+'/oa/act/actExTable/queryActExTableByPage.htm',
					toolbar:buttons,
					columns:config.column,
					eid:"dg1",
					args:"searchform"
			};
			using(["datagrid","parser","textbox","dialog","combobox","messager"],function(){
				initPage();
			});
		}
	}
});